package club.emergency.wechat.fastdfs.impl;

import club.emergency.wechat.common.response.StatusCode;
import club.emergency.wechat.common.response.ResponseEntity;
import club.emergency.wechat.common.utils.FastDfsStringUtil;
import club.emergency.wechat.fastdfs.FastDfsService;
import club.emergency.wechat.fastdfs.FastDfsStorageService;
import club.emergency.wechat.model.fastdfs.FastDFS;
import club.emergency.wechat.persistence.fastdfs.FastDfsMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * Create with IntelliJ IDEA
 * Project Name: wechat
 * Package Name: club.emergency.wechat.fastdfs.impl
 * Date: 2018/3/2713:38
 * User: 快乐的小菌菌
 * Description:
 *
 * @author jifujun
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class FastDfsServiceImpl implements FastDfsService, ApplicationContextAware {

    private static final int LIMIT_SIZE = 1073741824;

    private ApplicationContext applicationContext;

    private FastDfsMapper fastDfsMapper;

    public FastDfsServiceImpl(FastDfsMapper fastDfsMapper) {
        this.fastDfsMapper = fastDfsMapper;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Override
    public String upload(byte[] bytes, String extName, String fileName) {
        int size = bytes.length;
        if (size >= LIMIT_SIZE) {
            return ResponseEntity.ok(StatusCode.UPLOAD_FILE_SIZE_OVERFLOW, "上传的文件过大，请压缩后重试！");
        }
        String[] strings = getBean().uploadFile(bytes, extName);
        if (Objects.nonNull(strings)) {
            String storagePath = FastDfsStringUtil.stitchUploadPath(strings);
            log.info("上传的文件路径为：" + storagePath);
            FastDFS dfs = new FastDFS();
            dfs.setFileName(fileName);
            dfs.setExtName(extName);
            dfs.setSize(size);
            dfs.setStoragePath(strings[1]);
            dfs.setGroupName(strings[0]);
            fastDfsMapper.insert(dfs);
            return ResponseEntity.ok(StatusCode.UPLOAD_FILE_OK, "上传文件成功！", dfs);
        }
        return ResponseEntity.ok(StatusCode.UPLOAD_FILE_ERROR, "上传文件失败！", fileName + "." + extName);
    }

    @Override
    public String deleteById(Long id) {
        FastDFS dfs = fastDfsMapper.selectById(id);
        if (Objects.isNull(dfs)) {
            return ResponseEntity.ok(StatusCode.DELETE_FILE_OK, "文件不存在！跳过删除步骤！");
        }
        Integer code = getBean().deleteFile(dfs.getGroupName(), dfs.getStoragePath());
        if (!code.equals(0)) {
            return ResponseEntity.ok(StatusCode.DELETE_FILE_OK, "删除文件！");
        }
        fastDfsMapper.deleteById(id);
        return ResponseEntity.ok(StatusCode.DELETE_FILE_OK, "删除文件成功！");
    }

    private FastDfsStorageService getBean() {
        return applicationContext.getBean("fastDfsStorageServiceImpl", FastDfsStorageServiceImpl.class);
    }

}
